package com.haiyi.software.app;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.haiyi.software.app.basic.ActivityBasicFeedback;

public class ActivityFeedback extends ActivityBasicFeedback implements
		OnClickListener {

	private EditText Editx_feedback;
	private Button btn_select_type, btn_feedback;
	private TextView textView_type;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feedback);
		initCom();
	}

	private void initCom() {

		Editx_feedback = (EditText) this.findViewById(R.id.Editx_feedback);
		btn_select_type = (Button) this.findViewById(R.id.btn_select_type);
		btn_feedback = (Button) this.findViewById(R.id.btn_feedback);
		textView_type = (TextView) this.findViewById(R.id.textView_type);

		btn_feedback.setOnClickListener(this);
		btn_select_type.setOnClickListener(this);

		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
	}

	@Override
	protected void requestFinish(JsonObject jsonObjectReturn) {

		JsonObject jsobj = jsonObjectReturn;

		Toast.makeText(this, jsobj.get("desc").getAsString(), Toast.LENGTH_LONG)
				.show();

		if (jsobj.get("state").getAsString().equals("200")) {

			this.finish();
		}

	}

	@Override
	protected void requestError() {

		Toast.makeText(this, "网络超时", Toast.LENGTH_LONG).show();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_feedback:

			if (ckData()) {

				recommend(textView_type.getText().toString().trim(),
						Editx_feedback.getText().toString().trim());
			}
			break;

		case R.id.btn_select_type:

			getDialog().show();

			break;
		}
	}

	/*
	 * 检查数据
	 */
	private Boolean ckData() {

		Boolean bTxt = textView_type.getText().toString().trim().length() > 0;

		Boolean bEdit = Editx_feedback.getText().toString().trim().length() > 0;

		Boolean bEditMax = Editx_feedback.getText().toString().trim().length() <= 140;

		if (!bTxt) {
			Toast.makeText(this, "请选择反馈的类别", Toast.LENGTH_LONG).show();
		}
		if (!bEdit) {
			Toast.makeText(this, "请输入您要反馈的内容", Toast.LENGTH_LONG).show();
		}
		if (!bEditMax) {
			Toast.makeText(this, "反馈内容应该在140字符以内", Toast.LENGTH_LONG).show();
		}
		return bTxt & bEdit & bEditMax;
	}

	/**
	 * 生成对话框
	 * 
	 * @return
	 */
	private Builder getDialog() {

		final String[] str_menu = { "咨询", "建议", "故障", "投诉" };

		Builder builder = new AlertDialog.Builder(this);

		builder.setIcon(null);

		builder.setTitle("反馈类别");

		builder.setItems(str_menu, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				textView_type.setText(str_menu[which]);
			}

		});
		return builder;
	}

}
